<template>
  <div class="app-container" v-if="configList.length>0">

    <!-- 添加或修改微信客户端配置对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item :label="item.name"  v-for="(item,index) in configList" :key="index">
          <el-input v-model="item.value" placeholder="请输入" />
          <span style="color: #909399">{{item.remark}}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="rest">重 置</el-button>
      </div>
  </div>
</template>

<script>
import { saveConfig,viewConfig } from "@/api/wx/config";

export default {
  name: "WxConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 微信客户端配置表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        a: undefined,
        b: undefined,
        c: undefined,
        d: undefined,
        e: undefined,
        f: undefined,
        g: undefined,
        h: undefined,
        i: undefined,
        k: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      wxClientId: undefined
    };
  },
  created() {
    this.wxClientId = this.$route.query.id;
    this.getList();
  },
  methods: {
    /** 查询微信客户端配置列表 */
    getList() {
      this.loading = true;
      viewConfig({
        wxClientId: this.$route.query.id
      }).then(response => {
        this.configList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wxClientId: undefined,
        createTime: undefined,
        updateTime: undefined,
        a: undefined,
        b: undefined,
        c: undefined,
        d: undefined,
        e: undefined,
        f: undefined,
        g: undefined,
        h: undefined,
        i: undefined,
        k: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wxClientId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          saveConfig({
            wxClientId: this.wxClientId,
            configItems: this.configList
          }).then(response => {
            if (response.code === 200) {
              this.msgSuccess("保存成功");
            }else{
              this.msgError("保存失败");
            }
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wxClientIds = row.wxClientId || this.ids;
      this.$confirm('是否确认删除微信客户端配置编号为"' + wxClientIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delConfig(wxClientIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有微信客户端配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportConfig(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
